package cn.iocoder.yudao.module.train.controller.admin.staff;

import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.StaffUserDTO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.module.train.enums.ErrorCodeConstants.PHONE_NUMBER_TOOLONG;

import cn.iocoder.yudao.module.train.controller.admin.staff.vo.*;
import cn.iocoder.yudao.module.train.dal.dataobject.staff.StaffDO;
import cn.iocoder.yudao.module.train.service.staff.StaffService;

@Tag(name = "管理后台 - 工作人员用户")
@RestController
@RequestMapping("/train/staff")
@Validated
public class StaffController {

    @Resource
    private StaffService staffService;

    @PostMapping("/create")
    @Operation(summary = "创建工作人员用户")
    @PreAuthorize("@ss.hasPermission('train:staff:create')")
    public CommonResult<Integer> createStaff(@Valid @RequestBody StaffSaveReqVO createReqVO) {
        String phone = createReqVO.getStaffPhoneNumber();
        if (phone == null || !phone.matches("^1\\d{10}$")) {
            throw exception(PHONE_NUMBER_TOOLONG);
        }
        return success(staffService.createStaff(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工作人员用户")
    @PreAuthorize("@ss.hasPermission('train:staff:update')")
    public CommonResult<Boolean> updateStaff(@Valid @RequestBody StaffSaveReqVO updateReqVO) {
        String phone = updateReqVO.getStaffPhoneNumber();
        if (phone == null || !phone.matches("^1\\d{10}$")) {
            throw exception(PHONE_NUMBER_TOOLONG);
        }
        staffService.updateStaff(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工作人员用户")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('train:staff:delete')")
    public CommonResult<Boolean> deleteStaff(@RequestParam("id") Integer id) {
        staffService.deleteStaff(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除工作人员用户")
                @PreAuthorize("@ss.hasPermission('train:staff:delete')")
    public CommonResult<Boolean> deleteStaffList(@RequestParam("ids") List<Integer> ids) {
        staffService.deleteStaffListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工作人员用户")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('train:staff:query')")
    public CommonResult<StaffRespVO> getStaff(@RequestParam("id") Integer id) {
        StaffUserDTO staff = staffService.getStaff(id);
        return success(BeanUtils.toBean(staff, StaffRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工作人员用户分页")
    @PreAuthorize("@ss.hasPermission('train:staff:query')")
    public CommonResult<PageResult<StaffRespVO>> getStaffPage(@Valid StaffPageReqVO pageReqVO) {
        PageResult<StaffUserDTO> pageResult = staffService.getStaffPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StaffRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工作人员用户 Excel")
    @PreAuthorize("@ss.hasPermission('train:staff:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportStaffExcel(@Valid StaffPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StaffUserDTO> list = staffService.getStaffPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "工作人员用户.xls", "数据", StaffRespVO.class,
                        BeanUtils.toBean(list, StaffRespVO.class));
    }

}
